#include "BSException/BSException.h"
#include "boost/system/system_error.hpp"
#include "Log/SystemLog.h"
#include "DllLoader.h"

using namespace SystemTool;
using namespace SystemTool::IO;

// Construct the share lib
DllLoader::DllLoader():
	m_bDisposed(false)
{

}

// Destory the share lib
DllLoader::~DllLoader()
{
	if (!GetDisposed())
	{
		SetDisposed(true);

		Unload();
	}
}

// Load the library
bool DllLoader::Load(std::string strLibFullPath)
{
	if (strLibFullPath.empty())
	{
		return false;
	}

	try
	{
		GetShareLib().load(strLibFullPath);

		if (!IsLoad())
		{
			return false;
		}
	}
	catch (boost::dll::fs::system_error& e)
	{
		std::string strErrorText = std::string("Failed to load library,error code:")
			+ std::to_string(e.code().value())
			+" error message:"
			+e.what();

		LOG_ERROR_EX(strErrorText);

		return false;
	}

	return true;
}

// Unload the library
void DllLoader::Unload()
{
	GetShareLib().unload();
}

// Is load the library
bool DllLoader::IsLoad()
{
	return GetShareLib().is_loaded();
}

// Is have the function
bool DllLoader::IsHasFunc(std::string strFuncName)
{
	if (strFuncName.empty())
	{
		return false;
	}

	return GetShareLib().has(strFuncName);
}
